<?php

class hotel_model extends Model {

   public function get_meal_plans_by_hotel($hotel_id) {

         try{
         $mealplan_sql = "SELECT *  FROM tbl_hotelmealplans  a inner join tbl_mealplans b on b.id=a.mealplanid where  b.status=1 and a.Hid={$hotel_id}";
        $mealplan = $this->db->query($mealplan_sql);
        if($mealplan->num_rows() > 0) {
                 return $mealplan_result = $mealplan->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    public function get_accomodations()
    {
        try{
             $acc_sql = "SELECT * FROM tbl_accomodations WHERE status = 1";

            $acc_arr = $this->db->query($acc_sql);
            if($acc_arr->num_rows() > 0) {
                 return $acc_arr_result = $acc_arr->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    public function get_destinations()
    {
        try{
             $dest_sql = "SELECT * FROM tbl_destinations WHERE status = 1";

            $dest_arr = $this->db->query($dest_sql);
            if($dest_arr->num_rows() > 0) {
                 return $dest_arr_result = $dest_arr->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
    public function get_hotel_list($accid,$limit,$start,$sortorder="asc",$cityid=null) {
      
         try{
            $hotel_sql = "select a.id as hotelid,
                    (select d.image from tbl_hotelimages d where d.hotelid=a.id limit 0,1) as image,
                    (select lvalue from tbl_lang b where b.lkey=a.hotelname and b.lang='{$this->session->userdata("language")}') as hotelname ,
                    (select lvalue from tbl_lang b where b.lkey=a.txtHdescriptions and b.lang='{$this->session->userdata("language")}') as hoteldesc ,
                    a.numstars as ratingcount,
                    a.city as city_id,
					a.price as price
                    from tbl_hotelmaster a  inner join tbl_hotelaccomodations c on a.id=c.Hid
                     where c.accid={$accid}
                    "
                        ;
                    if($cityid) {
                        $hotel_sql.= " and  a.city ={$cityid} ";
                    }
                     $hotel_sql.= " order by hotelname {$sortorder} limit ".$start.",".$limit;
      //  echo $hotel_sql;
        $hotel = $this->db->query($hotel_sql);
        if($hotel->num_rows() > 0) {
                 return $hotel_result = $hotel->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
    public function get_hotel_list_dest($dest_id,$limit,$start,$sortorder="asc",$cityid=null) {
      
         try{
            $hotel_sql = "select a.id as hotelid,
                    (select d.image from tbl_hotelimages d where d.hotelid=a.id limit 0,1) as image,
                    (select lvalue from tbl_lang b where b.lkey=a.hotelname and b.lang='{$this->session->userdata("language")}') as hotelname ,
                    (select lvalue from tbl_lang b where b.lkey=a.txtHdescriptions and b.lang='{$this->session->userdata("language")}') as hoteldesc ,
                    a.numstars as ratingcount
                    from tbl_hotelmaster a  inner join tbl_hoteldestinations c on a.id=c.Hid
                     where c.destid={$dest_id}
                    "
                        ;
                     if($cityid) {
                        $hotel_sql.= " and  a.city ={$cityid} ";
                    }
                     $hotel_sql.= " order by hotelname {$sortorder}  limit ".$start.",".$limit;
        $hotel = $this->db->query($hotel_sql);
        if($hotel->num_rows() > 0) {
                 return $hotel_result = $hotel->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
    public function get_hotel_list_count($accid) {

         try{
            $hotel_sql = "select  count(*) as count
                    from tbl_hotelmaster a  inner join tbl_hotelaccomodations c on a.id=c.Hid
                     where c.accid={$accid}
                    "
                        ;
        $hotel = $this->db->query($hotel_sql);
        if($hotel->num_rows() > 0) {
                 $hotel_result = $hotel->row_array();
                  return $hotel_result["count"];
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    public function get_hotel_list_dest_count($dest_id) {

         try{
            $hotel_sql = "select  count(*) as count
                    from tbl_hotelmaster a  inner join tbl_hoteldestinations c on a.id=c.Hid
                     where c.destid={$dest_id}
                    "
                        ;
        $hotel = $this->db->query($hotel_sql);
        if($hotel->num_rows() > 0) {
                 $hotel_result = $hotel->row_array();
                  return $hotel_result["count"];
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
    public function get_offer_list_count($offer_type_id='all') {

         try{
             $offer_type_id=  strtoupper($offer_type_id);
             if ($offer_type_id!='ALL'){
                 $where=" and offer_type_id='{$offer_type_id}' ";
             }
            $sql = "select  count(*) as count
                    from tbl_offer 
                    where status=1 and
                    curdate( )
                    BETWEEN offer_start
                    AND offer_end 
                    {$where}
                    "
                        ;
        $offers = $this->db->query($sql);
        if($offers->num_rows() > 0) {
                 $offer_result = $offers->row_array();
                  return $offer_result["count"];
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }    
    
    public function get_offer_list($limit,$start, $hotel_id=0, $offer_type_id='all') {
      
         try{
             if ($hotel_id>0){
                 $where=" and hotel_id={$hotel_id} ";
             }
             $offer_type_id=  strtoupper($offer_type_id);
             if ($offer_type_id!='ALL'){
                 $where.=" and offer_type_id='{$offer_type_id}' ";
             }
            $offer_sql = "select offer_id,   
                    hotel_id,
                    image,
                    (select lvalue from tbl_lang b where b.lkey=a.title and b.lang='{$this->session->userdata("language")}' and module='offer') as title ,
                    (select lvalue from tbl_lang b where b.lkey=a.desc and b.lang='{$this->session->userdata("language")}' and module='offer') as `desc` ,
                    offer_start,
                    offer_end
                    from tbl_offer a                    
                     where status=1  and
                    curdate( )
                    BETWEEN offer_start
                    AND offer_end {$where}
                    "
                        ;
                    if (!$where){
                     $offer_sql.= " limit ".$start.",".$limit;
                    }
                     //echo $offer_sql;
        $hotel = $this->db->query($offer_sql);
        if($hotel->num_rows() > 0) {
                 return $hotel_result = $hotel->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
    public function get_offers(){
        try{             
            $offer_sql = "select offer_id,   
                    hotel_id,
                    image,
                    (select lvalue from tbl_lang b where b.lkey=a.title and b.lang='{$this->session->userdata("language")}' and module='offer') as title ,
                    (select lvalue from tbl_lang b where b.lkey=a.desc and b.lang='{$this->session->userdata("language")}' and module='offer') as `desc` ,
                    (select lvalue from tbl_lang b where b.lkey=c.hotelname and b.lang='{$this->session->userdata("language")}') as hotelname, 
                    offer_start,
                    offer_end
                    from tbl_offer a  
                    inner join tbl_hotelmaster c on a.hotel_id=c.id
                     where a.status=1  and
                    curdate( )
                    BETWEEN offer_start
                    AND offer_end
                    ";
                    
                     //echo $offer_sql;
        $offers = $this->db->query($offer_sql);
        if($offers->num_rows() > 0) {
                 return $offer_result = $offers->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
    public function get_hotel_details_by_hotel_id($hotelid,$lang='UK') {

         try{
            $hotel_sql = "select a.id as hotelid,
                    (select d.image from tbl_hotelimages d where d.hotelid=a.id limit 0,1) as image,
                    (select lvalue from tbl_lang b where b.lkey=a.hotelname and b.lang='{$lang}') as hotelname ,
                    (select lvalue from tbl_lang b where b.lkey=a.txtHdescriptions and b.lang='{$lang}') as hoteldesc ,
                     (select lvalue from tbl_lang b where b.lkey=a.htmltitletag and b.lang='{$lang}') as htmltitletag ,
                     (select lvalue from tbl_lang b where b.lkey=a.metadescription and b.lang='{$lang}') as metadescription ,
                     (select lvalue from tbl_lang b where b.lkey=a.metakeywords and b.lang='{$lang}') as metakeywords ,
                    a.numstars as ratingcount,
                    a.price as price,
                    a.mapurl as mapurl
                    from tbl_hotelmaster a
                     where a.id={$hotelid}
                    "
                        ;
        $hotel = $this->db->query($hotel_sql);
        if($hotel->num_rows() > 0) {
                 return $hotel_result = $hotel->row_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

   
    public function get_hotel_details($accid)
    {
        
         try{
           $hotel_sql = "SELECT *
                                    FROM tbl_hotels where id=?";
        $hotel = $this->db->query($hotel_sql, array($accid));
        if($hotel->num_rows() > 0) {
                 return $hotel_result = $hotel->row_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    public function get_hotel_facilities($hotelid) {

         try{
            $hotel_sql = "select  id , Hfacility from tbl_facility  where id in (select fid from tbl_hotelfacility where Hid={$hotelid}) "
                        ;
        $hotel = $this->db->query($hotel_sql);
        if($hotel->num_rows() > 0) {
                 return $hotel_result = $hotel->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    public function get_hotel_images($hotelid) {

         try{
            $hotel_sql = "select  image  as image from tbl_hotelimages where hotelid={$hotelid} "
                        ;
        $hotel = $this->db->query($hotel_sql);
        if($hotel->num_rows() > 0) {
                 return $hotel_result = $hotel->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
   
    public function get_hotel_content($hotelid) {

         try{
            $hotel_sql = "select
                    (select lvalue from tbl_lang b where b.lkey=a.location and b.lang='{$this->session->userdata("language")}') as location ,
                    (select lvalue from tbl_lang b where b.lkey=a.about and b.lang='{$this->session->userdata("language")}') as about ,
                     (select lvalue from tbl_lang b where b.lkey=a.accommodation and b.lang='{$this->session->userdata("language")}') as accomodation ,
                     (select lvalue from tbl_lang b where b.lkey=a.facilities and b.lang='{$this->session->userdata("language")}') as facilities ,
                     (select lvalue from tbl_lang b where b.lkey=a.excursion and b.lang='{$this->session->userdata("language")}') as excursion
                    from tbl_hotelcontent a   where hotelid={$hotelid} "
                        ;
        $hotel = $this->db->query($hotel_sql);
        if($hotel->num_rows() > 0) {
                 return $hotel_result = $hotel->row_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }
    
    public function get_featured_accomodation()
    {
        try{
             $facc_sql = "SELECT *
                            FROM tbl_featuredaccommodation
                            WHERE curdate( )
                            BETWEEN date_from
                            AND date_to
                            AND isactive =1";

            $acc_arr = $this->db->query($facc_sql);            
            if($acc_arr->num_rows() > 0) {
                 return $acc_arr_result = $acc_arr->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

    public function get_featured_destination()
    {
        try{
             $fdest_sql = "SELECT *
                            FROM tbl_featureddestination
                            WHERE curdate( )
                            BETWEEN date_from
                            AND date_to
                            AND isactive =1";

            $dest_arr = $this->db->query($fdest_sql);            
            if($dest_arr->num_rows() > 0) {
                 return $dest_arr_result = $dest_arr->result_array();
            } else {
                return FALSE;
            }
        } catch(Exception $e){
            throw new Exception('Error in DB query');
            return FALSE;
        }
    }

            }
?>
